package com.whc.common.interceptors;


import com.whc.common.context.BaseContext;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;



@Slf4j
public class UserInfoInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 1.获取请求头中的用户信息
        String userId = request.getHeader("user-info");
        System.out.println("是否到了这里："+userId);
        if(userId!=null&&!"".equals(userId)){
            BaseContext.setCurrentId(Long.valueOf(userId));
            log.info("mvc进行了拦截并获取了用户id"+userId);
        }else{
            log.info("mvc没有拦截到用户id");
        }
        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        BaseContext.removeCurrentId();
    }

}
